package com.xiaolee.hotool.json;

import java.util.List;

public abstract class TreeNode {
    private String name;
    private TreeNode parent;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    /**
     *
     * @param iterator
     */
    public void putByPath(PathIterator iterator, Object value) {
        putByPath(iterator, -1, value);
    }

    /**
     * 在当前节点的 index子节点位置, 解析是否有某个路径的属性
     *
     * @param iterator
     * @param index
     */
    public void putByPath(PathIterator iterator, int index, Object value) {
        PathSegment segment = iterator.get();

        TreeNode node = createNodeIfAbsent(iterator, index, value);

        if (iterator.hasNext()) {
            node.putByPath(iterator.next(), segment.getArrayIndex(), value);
        }
    }

    abstract TreeNode createNodeIfAbsent(PathIterator iterator, int index, Object value);

    abstract List<TreeNode> children();

    abstract TreeNode child(String name);

    abstract Object value();
}
